//
//  BaseNavigationController.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/7/27.
//

import UIKit

protocol SwipeBackDisableable {
    
    func shouldDisableSwipeBack() -> Bool
}

class BaseNavigationController: UINavigationController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.delegate = self
        self.delegate = self
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let topViewController = self.topViewController as? SwipeBackDisableable {
            return !topViewController.shouldDisableSwipeBack()
        }
        return viewControllers.count > 1
    }
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if let viewController = viewController as? SwipeBackDisableable {
            self.interactivePopGestureRecognizer?.isEnabled = !viewController.shouldDisableSwipeBack()
        } else {
            self.interactivePopGestureRecognizer?.isEnabled = true
        }
    }
}
